//
//  client_service_request.cpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 



#include "client_service_request.hpp"
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/client_processor.hpp"
#include "components/protocol/client_service_request.hpp"
namespace components{
namespace protocol{


bool client_service_request::cancel()
{

    try{
        bool retcode=true;
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__;
        retcode &= (create_enc_service_request_ && create_enc_service_request_->cancel());
        retcode &= (send_enc_pb_message_ && send_enc_pb_message_->cancel());
        return retcode;
    }
    catch(std::exception &e){
        LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
        return false;
    }


}

client_service_request::client_service_request(ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key,
                                               ba::ip::udp::socket& msocket, udp_endpoint_t& local_if, udp_endpoint_t& mgroup,
                                               stats_message_client_processor &message_processor)
    :ios_(ios), iv_(iv), key_(key), msocket_(msocket), local_if_(local_if), mgroup_(mgroup),message_processor_(message_processor)
{

    name_=std::string(__FUNCTION__);
    DLOG(INFO)<<name_<<"()";
    init_components();

}

boost::shared_ptr<client_service_request> client_service_request::create(ba::io_service& ios, crypto_buf_t& iv, crypto_buf_t& key,
                                                                         ba::ip::udp::socket& msocket, udp_endpoint_t& local_if, udp_endpoint_t& mgroup,
                                                                         stats_message_client_processor &message_processor)
{

    return boost::shared_ptr<client_service_request>(new client_service_request(ios,iv,key,msocket,local_if,mgroup,message_processor));
}

void client_service_request::handle_create_enc_service_request(const crypto_error& eec)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<eec.what()<<"]";

    if(!phdl::is_error(eec))
    {

    }

    //send_enc_pb_message_ && send_enc_pb_message_->cancel();


}

void client_service_request::handle_send_enc_pb_message(const bs::error_code& ec,std::size_t bytes_transferred)
{


    LOG(INFO)<<name_<<"::"<<__FUNCTION__<<"["<<ec<<"]";

    if(!ec)
    {
        LOG(INFO)<<__FUNCTION__<<":ec:\t"<<ec ;
        LOG(INFO)<<__FUNCTION__<<":bytes_transferred:\t"<<bytes_transferred ;

    }

    //create_enc_service_request_ && create_enc_service_request_->cancel();


}

void client_service_request::init_components()
{

    if(!create_enc_service_request_) create_enc_service_request_ = components::protocol::create_enc_service_request::create(ios_,iv_,key_,message_processor_);
    if(!send_enc_pb_message_) send_enc_pb_message_ = components::network::send_enc_pb_message::create(ios_,msocket_,local_if_,mgroup_);
}

client_service_request::~client_service_request()
{
    DLOG(INFO)<<name_<<"::"<<__FUNCTION__<<std::endl;
}
}//end namespace protocol
}//end namespace components

